Macromedia Flex Article

Providing a Flex Front End to Your Struts Applications


Table of Contents

  1. Introduction
  2. Improving the View in Your MVC Architecture
  3. A Sample Struts Application
  4. Providing a Struts Application with a Flex Front End: Approach 1—HTTP Request/Reponse Integration
  5. Providing a Struts Application with a Flex Front End: Approach 2 SOAP Integration
  6. Conclusion

A Sample Struts Application

This article uses a simple application to demonstrate how you can use Flex with Struts. The sample application is a registration module that customers use to register with our fictive website.

The action is defined as follows in struts-config.xml:

<action
	path="/RegistrationSubmit"
	type="samples.struts.RegistrationAction"
	name="registrationForm"
	scope="request"
	validate="true"
	input="/registration/registration.jsp">
	<forward name="success" path="/registration/confirm.jsp"/>
</action>

The following diagram illustrates the workflow of the Registration application:

The workflow of the Registration application

Figure 2. The workflow of the Registration application.

The RegisterAction class is defined as follows:

package samples.struts;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class RegistrationAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, 
	HttpServletRequest request, HttpServletResponse response) throws Exception {

		ActionErrors errors = new ActionErrors();
		RegistrationForm registrationForm = (RegistrationForm) form;
		String email = registrationForm.getEmail();

		// Dummy business logic: here is where you would typically communicate with a business object
		if (email == null || email.indexOf("@") < 0) {
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.registration.failed"));
				saveErrors(request,errors);
				return new ActionForward(mapping.getInput());
		}

		request.setAttribute("registration", form);
		return mapping.findForward("success");
}

 }

Note: In this example, the Action implements dummy business logic: a simple e-mail address verification. In a real life scenario, the Action would probably delegate a database insertion to a business object.

The confirm.jsp page is defined as follows:

<%@ page import="java.util.Enumeration"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
Thank you for registering:<br>
<bean:write name="registration" property="firstName"/><br>
<bean:write name="registration" property="lastName"/><br>
<bean:write name="registration" property="phone"/><br>
<bean:write name="registration" property="email"/>